define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/user/index',
                    add_url: 'user/user/add',
                    edit_url: 'user/user/edit',
                    del_url: 'user/user/del',
                    multi_url: 'user/user/multi',
                    table: 'user',
                }
            });

            var table = $("#table");

            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);

                $("input[name='pid']", form)
                    .addClass("selectpage")
                    .data("source", "user/user/index")
                    .data("primaryKey", "id")
                    .data("field", "nickname")
                    .data("orderBy", "id desc");

                Form.events.cxselect(form);
                Form.events.selectpage(form);
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'user.id',
                search: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true},
                        {field: 'pid', title: __('推广人'), visible:false, operate: 'LIKE %...%'},
                        {field: 'parentuser.nickname', title: __('推广人'), operate: false},
                        {field: 'group.name', title: __('Group')},
                        {field: 'invite_code', title: __('邀请码'), operate: 'LIKE'},
                        {field: 'username', title: __('Username'), operate: 'LIKE'},
                        {field: 'nickname', title: __('Nickname'), operate: 'LIKE'},
                        // {field: 'email', title: __('Email'), operate: 'LIKE'},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'avatar', title: __('Avatar'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                        // {field: 'level', title: __('Level'), operate: 'BETWEEN', sortable: true},
                        // {field: 'gender', title: __('Gender'), visible: false, searchList: {1: __('Male'), 0: __('Female')}},
                        {field: 'money', title: __('Money'), operate: 'BETWEEN', sortable: true},
                        {field: 'score', title: __('Score'), operate: 'BETWEEN', sortable: true},
                        {field: 'successions', title: __('Successions'), visible: false, operate: 'BETWEEN', sortable: true},
                        {field: 'maxsuccessions', title: __('Maxsuccessions'), visible: false, operate: 'BETWEEN', sortable: true},
                        {field: 'logintime', title: __('Logintime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'loginip', title: __('Loginip'), formatter: Table.api.formatter.search},
                        {field: 'jointime', title: __('Jointime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'joinip', title: __('Joinip'), formatter: Table.api.formatter.search},
                        {field: 'status', title: __('Status'), formatter: Table.api.formatter.status, searchList: {normal: __('Normal'), hidden: __('Hidden')}},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            // buttons:[
                            //     {
                            //         name: 'detail',
                            //         text: '推广详情',
                            //         title: '推广详情',
                            //         classname: 'btn btn-xs btn-primary btn-dialog',
                            //         icon: 'fa fa-sticky-note-o',
                            //         url: function (res) {
                            //             return 'user/user/detail/ids/' + res.id;
                            //         },
                            //         callback: function (data) {
                            //             Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                            //         },
                            //         visible: function (row) {
                            //             return true;
                            //         }
                            //     },
                            //     {
                            //         name: 'setuser2',
                            //         text: '设置成门店',
                            //         title: '设置成门店',
                            //         classname: 'btn btn-xs btn-success btn-ajax',
                            //         // icon: 'fa fa-sticky-note-o',
                            //         confirm: '是否设定该用户成为门店?',
                            //         url: function (res) {
                            //             return 'user/user/setuser2/ids/' + res.id;
                            //         },
                            //         callback: function (data) {
                            //             Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                            //         },
                            //         visible: function (row) {
                            //             if (row.group_id === 1) {
                            //                 return true;
                            //             }
                            //         },
                            //         refresh: true
                            //     }
                            //     ,{
                            //         name: 'setuser3',
                            //         text: '设置成分公司',
                            //         title: '设置成分公司',
                            //         classname: 'btn btn-xs btn-info btn-ajax',
                            //         // icon: 'fa fa-sticky-note-o',
                            //         confirm: '是否设定该用户成为分公司?',
                            //         url: function (res) {
                            //             return 'user/user/setuser3/ids/' + res.id;
                            //         },
                            //         callback: function (data) {
                            //             Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                            //         },
                            //         visible: function (row) {
                            //             if(row.group_id === 2) {
                            //                 return true;
                            //             }
                            //         },
                            //         refresh: true
                            //     }
                            //     ,{
                            //         name: 'setuser4',
                            //         text: '设置成合伙人',
                            //         title: '设置成合伙人',
                            //         classname: 'btn btn-xs btn-warning btn-dialog',
                            //         // icon: 'fa fa-sticky-note-o',
                            //         confirm: '是否设定该用户成为合伙人?',
                            //         url: function (res) {
                            //             return 'user/user/setuser4/ids/' + res.id;
                            //         },
                            //         callback: function (data) {
                            //             Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                            //         },
                            //         visible: function (row) {
                            //             if(row.group_id === 1) {
                            //                 return true;
                            //             }
                            //         },
                            //         refresh: true
                            //     }
                            // ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        detail: function () {
            Controller.api.bindevent();
        },
        setuser1: function () {
            Controller.api.bindevent();
        },
        setuser2: function () {
            Controller.api.bindevent();
        },
        setuser3: function () {
            Controller.api.bindevent();
        },
        setuser4: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});